// videodetail.js 视频详情
var playnum = 0;
var app = getApp()
var token = '';
var sceneres = {}
var shihan = require('../../../common/fetch');
var config = require('../../../common/config');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    page_size: 20,
    foldword: '查看更多',
    videoId: '',
    reviewflag: true,
    videoReview: [],
    videocontro: true,
    showLoading: true,
    foldactive: false,
    toast_flag: false,
    collectionflag: false,
    playvideoflag: true,
    videopromptflag: true,
    defaults: 'https://img-oss.shihanbainian.com/shihan2/index/defaults.png'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    console.log(options)
    if (options.scene) {
      var scene = decodeURIComponent(options.scene).split('&'); //id=22&fansid=1694
      if (scene && scene.length > 0) {
        for (var i = 0; i < scene.length; i++) {
          var str = scene[i].split('=')
          if (str[0] == 'id') {
            that.setData({
              videoId: str[1]
            })
          } else if (str[0] == 'pid' || str[0] == 'fansid') {
            sceneres['partner_id'] = str[1]
          } else if (str[0] == 'cid') {
            sceneres['cid'] = str[1]
          }
        }
      }
    } else {
      that.setData({
        videoId: options.id
      })
      if (options.cid) {
        sceneres['cid'] = options.cid
      }
      if (options.fansid) {
        sceneres['partner_id'] = options.fansid
      } else if (options.pid) {
        sceneres['partner_id'] = options.pid
      } else if (options.partner_id) {
        sceneres['partner_id'] = options.partner_id
      }
    }
    that.getreview(that.data.videoId)
    that.getVideo(that.data.videoId)

    var pages = getCurrentPages();
    if (pages.length >= 2) {
      var prevPage = pages[pages.length - 2];
      prevPage.setData({
        videosferesh: true,
        promotefresh: false
      })
    }

    console.log(sceneres)
    app.wechatSwitch().then(function(wechat_switch) { //是否打开微信授权开关
      app.commonLogin(sceneres, wechat_switch)
    })
  },
  backhome: function() {
    wx.switchTab({
      url: '/pages/home/home'
    });
  },
  getVideo: function(a) { //video_comment_add
    var that = this
    shihan.sendRequest(config.videodetail + a + '.html', 'GET', {})
      .then(function(response) {
        if (response.data.code == 0) {
          that.setData({
            videodetail: response.data.data,
            floatAd: response.data.data.sh_float_ad,
            votestatus: response.data.data.sh_video.sh_is_vote_button,
            showLoading: false
          })
        } else {
          wx.showModal({
            title: "提示",
            content: response.data.msg,
            confirmText: "确认",
            confirmColor: "#c40000",
            showCancel: false,
            success: function(res) {
              if (res.confirm) {
                wx.navigateBack({})
              }
            }
          })
        }
      }, function(error) {
        console.log(error);
      });
  },
  closeprompt: function() {
    this.setData({
      videopromptflag: false
    })
  },
  onreviewInput: function(e) { // 搜索输入值时的操作
    token = wx.getStorageSync('token')
    if (token) {
      this.setData({
        reviewdetail: e.detail.value
      });
    } else {
      app.commonNoLogin()
    }
  },
  onreviewConfirm: function() { //video_comment_add
    var that = this
    token = wx.getStorageSync('token')
    if (token) {
      shihan.sendRequest(config.video_comment_add, 'POST', {
          id: that.data.videoId,
          detail: that.data.reviewdetail
        })
        .then(function(response) {
          // console.log(response)
          if (response.data.code == 0) {
            that.setData({
              page: 1,
              page_size: 20,
              reviewflag: true,
              reviewdetail: '',
              videoReview: []
            })
            var scrollflag = true
            that.getreview(that.data.videoId, scrollflag)
          } else {
            shihan.showToast.call(that, response.data.msg);
          }
        }, function(error) {
          console.log(error);
        });
    } else {
      app.commonNoLogin()
    }
  },
  lookreview: function() {
    this.setData({
      scrollchild: 'reviewbox'
    })
  },
  addcollection: function(e) {
    var that = this, token = wx.getStorageSync('token');
    if (token) {
      if (that.data.videodetail.sh_video.sh_is_collect == 0) {
        shihan.sendRequest(config.add_favorite, 'POST', {
            id: that.data.videoId,
            type: 5
          })
          .then(function(response) {
            console.log(response);
            if (response.data.code == 0) {
              that.data.videodetail.sh_video.sh_is_collect = 1
              that.setData({
                collectionflag: true,
                videodetail: that.data.videodetail
              })
              shihan.showToast.call(that, '添加收藏成功');
            } else {
              shihan.showToast.call(that, response.data.msg);
            }
          }, function(error) {
            console.log(error);
          });
      } else {
        shihan.sendRequest(config.remove_favorite, 'POST', {
            id: that.data.videoId,
            type: 5
          })
          .then(function(response) {
            console.log(response);
            if (response.data.code == 0) {
              that.data.videodetail.sh_video.sh_is_collect = 0
              that.setData({
                collectionflag: false,
                videodetail: that.data.videodetail
              })
              shihan.showToast.call(that, '取消收藏成功');
            } else {
              shihan.showToast.call(that, response.data.msg);
            }
          }, function(error) {
            console.log(error);
          });
      }
    } else {
      app.commonNoLogin()
    }
  },
  votecast: function(e) {
    var that = this, token = wx.getStorageSync('token');
    if (token) {
      if (that.data.videodetail.sh_video.sh_is_vote == 0) {
        shihan.sendRequest(config.video_vote_add, 'POST', {
            id: that.data.videoId
          })
          .then(function(response) {
            console.log(response);
            if (response.data.code == 0) {
              that.data.videodetail.sh_video.sh_is_vote = 1
              that.setData({
                votestatus: response.data.data,
                videodetail: that.data.videodetail,
              })
              that.getVideo(that.data.videoId);
            }
          }, function(error) {
            console.log(error);
          });
      }
    } else {
      app.commonNoLogin()
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    this.setData({
      playvideoflag: true
    })
    playnum = 0
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    this.setData({
      playvideoflag: true
    })
    playnum = 0
  },
  openfold: function() {
    if (this.data.foldactive == false) {
      this.setData({
        foldactive: true,
        foldword: '收起'
      })
    } else {
      this.setData({
        foldactive: false,
        foldword: '查看更多'
      })
    }
  },
  opengoodsdetail: function(e) {
    wx.navigateTo({
      url: '/pages/goodsdetail/goodsdetail?goodsid=' + e.currentTarget.dataset.id
    })
  },
  openstoredetail: function(e) {
    wx.navigateTo({
      url: '/pages/storedetail/storedetail?type=brand&id=' + e.currentTarget.dataset.id
    })
  },
  lookvideorank: function() {
    wx.navigateTo({
      url: '/pages/other/videorank/videorank'
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      page: 1,
      page_size: 20,
      reviewflag: true,
      videoReview: []
    })
    this.getreview(this.data.video_id)
  },
  getreview: function(id, scrollflag) {
    var that = this
    shihan.sendRequest(config.videocomment + id + '/' + that.data.page + '-' + that.data.page_size + '.html', 'GET', {})
      .then(function(response) {
        if (response.data.code == 0 && response.data.data.sh_items.length > 0) {
          that.setData({
            reviewflag: true,
            video_id: id,
            page: that.data.page + 1,
            videoreviewlength: response.data.data.sh_total_rows,
            videoReview: that.data.videoReview.concat(response.data.data.sh_items)
          })
          if (scrollflag == true) {
            that.setData({
              scrollchild: 'reviewbox'
            })
          }
        } else {
          that.setData({
            reviewflag: false
          })
        }
        wx.stopPullDownRefresh()
      }, function(error) {
        console.log(error);
      });
  },
  loadMorereview: function() {
    if (this.data.reviewflag) {
      this.setData({
        reviewflag: false
      })
      this.getreview(this.data.video_id)
    }
  },
  playvideo: function() {
    token = wx.getStorageSync('token')
    if (token) {
      var that = this
      if (playnum == 0) {
        shihan.sendRequest(config.video_play_num_add, 'POST', { id: that.data.videoId })
          .then(function(response) {
            console.log(response);
            if (response.data.code == 0) {
              playnum++
              that.setData({
                playvideoflag: false,
                videopromptflag: false
              })
            } else {
              shihan.showToast.call(that, response.data.msg);
            }
          }, function(error) {
            console.log(error);
          });
      }
      var videoContext = wx.createVideoContext('myVideo');
      videoContext.play();
      // this.votecast();  
    } else {
      app.commonNoLogin()
    }
  },
  jumpadvert: function(e) { //广告位跳转
    app.AdSkipper(e)
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    var that = this,
      sharetit = '',
      shareurl;
    var customerId = wx.getStorageSync('customerId');
    if (that.data.videodetail.sh_video.sh_name) {
      sharetit = that.data.videodetail.sh_video.sh_name
    }
    shareurl = '/pages/other/videodetail/videodetail?id=' + that.data.videodetail.sh_video.sh_id + '&pid=' + customerId
    return {
      title: sharetit,
      path: shareurl,
      // imageUrl: that.data.storedetail.sh_seller.sh_share_image,
      success: function(res) {
        // 转发成功
      },
      fail: function(res) {
        // 转发失败
      }
    }
  }
})